package com.curvebreakers.wheresmystuff.activities;

import java.util.List;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.model.DBResultListener;
import com.curvebreakers.wheresmystuff.model.DatabaseModel;
import com.curvebreakers.wheresmystuff.model.Entry;
import com.curvebreakers.wheresmystuff.model.EntryAdapter;
import com.curvebreakers.wheresmystuff.model.Session;

public class DashboardFragment extends Fragment {
	private View view;
	private EntryAdapter myEntriesAdapter;
	private EntryAdapter myMatchesAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (view == null) {
			myEntriesAdapter = new EntryAdapter(getActivity(), 0);
			myMatchesAdapter = new EntryAdapter(getActivity(), 0);

			DatabaseModel.getUserEntries(Session.getSession().getUser().getId(), new MyEntriesDBListener());
			view = inflater.inflate(R.layout.fragment_dashboard, container,false);
//			LinearLayout myEntries = ((LinearLayout) view.findViewById(R.id.myEntries));
//			myEntries.setEmptyView(view.findViewById(R.id.myEntriesEmpty));
//			myEntries.setAdapter(myEntriesAdapter);
//			myEntries.setOnItemClickListener(new MyEntriesClickListener());
//			ListView myMatches = ((ListView) view.findViewById(R.id.matches));
//			myMatches.setEmptyView(view.findViewById(R.id.matchesEmpty));
//			myMatches.setAdapter(myMatchesAdapter);
//			myMatches.setOnItemClickListener(new CustomEntryClickListener(myMatchesAdapter));
			
			((TextView) view.findViewById(R.id.welcomeLabel)).setText(Session.getSession().getUser().getEmail());
			
			final Button button = (Button) view.findViewById(R.id.editButton);
			button.setOnClickListener(new View.OnClickListener() {           
			        public void onClick(View v) {
			        	Intent changePage = new Intent(getActivity(),EditProfileActivity.class);
			    		startActivity(changePage);
			        }
			});
		}
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		DatabaseModel.getUserEntries(Session.getSession().getUser().getId(), new MyEntriesDBListener());
		DatabaseModel.getUserMatches(Session.getSession().getUser().getId(), new MyMatchesDBListener());
	}
		
	/**
	 * DBResultListener implementation that fills the my entries LinearLayout
	 */
	private class MyEntriesDBListener implements DBResultListener {

		@SuppressWarnings("unchecked")
		@Override
		public void onDBResultReceived(Object obj) {
			myEntriesAdapter.clear();
			myEntriesAdapter.addAll((List<Entry>) obj);
			LinearLayout myEntries = ((LinearLayout) view.findViewById(R.id.myEntries));
			myEntries.removeAllViews();

			for (int i = 0; i < myEntriesAdapter.getCount(); i++) {
				if (i > 0) {
					View separator = new View(DashboardFragment.this.getActivity());
					separator.setBackgroundColor(Color.rgb(204, 204, 204));
					separator.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
					myEntries.addView(separator);
				}
				View v = myEntriesAdapter.getView(i, null, myEntries);
				int top = v.getPaddingTop();
				int left = v.getPaddingLeft();
				int right = v.getPaddingRight();
				int bottom = v.getPaddingBottom();
				v.setBackgroundResource(R.drawable.selectable_background_wmstheme);
				v.setPadding(left, top, right, bottom);
				v.setOnClickListener(new EntryClickListener(myEntriesAdapter.getItem(i)));
				myEntries.addView(v);
			}
			View myEntriesEmpty = view.findViewById(R.id.myEntriesEmpty);
			if (myEntries.getChildCount() > 0) {
				myEntriesEmpty.setVisibility(View.GONE);
			} else {
				myEntriesEmpty.setVisibility(View.VISIBLE);
			}
		}

		@Override
		public void onDBRequestFailure(String message) {
			System.out.println(message);
		}
		
	}
	
	/**
	 * DBResultListener implementation that fills the my matches LinearLayout
	 */
	private class MyMatchesDBListener implements DBResultListener {

		@SuppressWarnings("unchecked")
		@Override
		public void onDBResultReceived(Object obj) {
			myMatchesAdapter.clear();
			myMatchesAdapter.addAll((List<Entry>) obj);
			
			LinearLayout matches = ((LinearLayout) view.findViewById(R.id.matches));
			matches.removeAllViews();

			for (int i = 0; i < myMatchesAdapter.getCount(); i++) {
				if (i > 0) {
					View separator = new View(DashboardFragment.this.getActivity());
					separator.setBackgroundColor(Color.rgb(204, 204, 204));
					separator.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
					matches.addView(separator);
				}
				View v = myMatchesAdapter.getView(i, null, matches);
				int top = v.getPaddingTop();
				int left = v.getPaddingLeft();
				int right = v.getPaddingRight();
				int bottom = v.getPaddingBottom();
				v.setBackgroundResource(R.drawable.selectable_background_wmstheme);
				v.setPadding(left, top, right, bottom);
				v.setOnClickListener(new EntryClickListener(myMatchesAdapter.getItem(i)));
				matches.addView(v);
			}
			View matchesEmpty = view.findViewById(R.id.matchesEmpty);
			if (matches.getChildCount() > 0) {
				matchesEmpty.setVisibility(View.GONE);
			} else {
				matchesEmpty.setVisibility(View.VISIBLE);
			}
		}

		@Override
		public void onDBRequestFailure(String message) {
			System.out.println(message);
		}
		
	}
	
	/**
	 * View.OnClickListener implementation that opens up the EntryDetailActivity on the click of an entry
	 */
	private class EntryClickListener implements View.OnClickListener {
		Entry entry;
		
		public EntryClickListener(Entry entry) {
			this.entry = entry;
		}
		
		@Override
		public void onClick(View v) {
			Intent detailIntent = new Intent(DashboardFragment.this.getActivity(), EntryDetailActivity.class);
			detailIntent.putExtra("entry", entry);
			startActivity(detailIntent);
		}

		
	}
}
